package cn.xspace.wnx.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.xspace.android.xafcommon.appliction.BaseApplication;
import com.xspace.android.xafcommon.util.LogUtil;

import cn.xspace.library.util.MyInfoUtil;


public class WNXDBHelper extends SQLiteOpenHelper {
    private static final String TAG = WNXDBHelper.class.getSimpleName();
    private static WNXDBHelper instance;
    /****
     */
    private static final int DB_VERSION = 2; //数据库有变动的时候编号需要加1


    private WNXDBHelper(Context context) {
        super(context, getUserDatabaseName(), null, DB_VERSION);
    }

    public static WNXDBHelper getInstance(Context context) {
        LogUtil.d(TAG, "getInstance ");
        if (instance == null) {
            instance = new WNXDBHelper(context);
        }
        return instance;
    }

    private static String getUserDatabaseName() {
        LogUtil.d(MyInfoUtil.getMyMobile(BaseApplication.mInstance));
        return "qsfy_wnx" + MyInfoUtil.getMyMobile(BaseApplication.mInstance) + ".db";
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // 版本1
        LogUtil.d(TAG, "onCreate");
        LogUtil.d(TAG, "sql >>" + MessageRecordDao.MSG_LIST_TABLE_CREATE);
        db.execSQL(MessageRecordDao.MSG_LIST_TABLE_CREATE);
        LogUtil.d(TAG, "MessageRecordDao sql >>" + ChatRecordDao.MSG_RECORD_TABLE_CREATE);
        db.execSQL(ChatRecordDao.MSG_RECORD_TABLE_CREATE);
//        db.execSQL(WNXMessageListDao.WNX_LIST_TABLE_CREATE);
        db.execSQL(ChatFileDao.FIlE_RECORD_TABLE_CREATE);
        LogUtil.d(TAG, "ChatFileDao sql >>" + ChatFileDao.FIlE_RECORD_TABLE_CREATE);
//        db.execSQL(WeinongxinListDao.MSG_LIST_TABLE_CREATE);
        db.execSQL(OrgAllUserDao.ORG_USER_TABLE_CREATE);
        db.execSQL(NoticeDao.WNX_LIST_TABLE_CREATE);
        db.execSQL(PreferencesDb.CREATE_TABLE);
        // 版本2
        db.execSQL(MyNewFriendDao.NEW_FRIEND_TABLE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        switch (oldVersion) {
            case 1:
                //由版本1升级到版本2
                if (newVersion == 2) {
                    db.execSQL(MyNewFriendDao.NEW_FRIEND_TABLE_CREATE);
                    db.beginTransaction();
                    db.execSQL(ChatRecordDao.DROP_TEMP_TABLE);
                    db.execSQL(ChatRecordDao.RENAME_TO_TEMP_TABLE);
                    db.execSQL(ChatRecordDao.MSG_RECORD_TABLE_CREATE);
                    db.execSQL(ChatRecordDao.COPY_TEMP_TO_NEW_TABLE);
                    db.setTransactionSuccessful();
                    db.endTransaction();
                }
                break;
            default:
                break;
        }
    }


    public void closeDB() {
        if (instance != null) {
            try {
                SQLiteDatabase db = instance.getWritableDatabase();
                db.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            instance = null;
        }
    }
}
